package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.service.SettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class SettingsController {

    @Autowired
    private SettingsService settingsService;

    /**
     * 查询通用设置
     *
     * @return
     */
    @GetMapping("/settings")
    public ResponseEntity settings() {
        SettingsVo vo = settingsService.settings();
        return ResponseEntity.ok(vo);
    }
    /**
     * 设置陌生人问题
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        //获取问题
        String content = (String) map.get("content");
        settingsService.saveQuestion(content);
        return ResponseEntity.ok(null);
    }
    /**
     * 通知设置
     *
     * @param map
     * @return
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map map) {
        settingsService.saveSettings(map);
        return ResponseEntity.ok(null);

    }
    /**
     * 黑名单分页查询
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = settingsService.blacklist(page, pagesize);
        return ResponseEntity.ok(result);
    }
    /**
     * 移除黑名单
     *
     * @param blackUserId
     * @return
     */
    @DeleteMapping("/blacklist/{id}")
    public ResponseEntity deleteBlackList(@PathVariable("id") Long blackUserId) {
        settingsService.deleteBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }
    /**
     * 更新头像
     * @param headPhoto
     * @throws IOException
     */
    @PostMapping("/header")
    public void updateHead(MultipartFile headPhoto) throws IOException {
        settingsService.updateHeader(headPhoto);
    }
    /**
     * 修改手机号一：发送验证码
     *
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        //调用server方法发送验证码
        ResponseEntity responseEntity = settingsService.sendVerificationCode();
        return responseEntity;
    }
    /**
     * 修改手机号二：校验验证码
     *
     * @param map
     * @return
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        String code = (String) map.get("verificationCode");
        ResponseEntity responseEntity = settingsService.checkVerificationCode(code);
        return responseEntity;
    }
    /**
     * 修改手机号三：保存手机号
     *
     * @return
     */
    @PostMapping("/phone")
    public void phoneUpdate(@RequestBody Map map ) {
        String phone = (String) map.get("phone");
        settingsService.phoneUpdate(phone);
    }
}
